package edu.nf.meituan.service.rider.impl;

import edu.nf.meituan.dao.rider.impl.RiderDAOImpl;
import edu.nf.meituan.model.rider.Rider;
import edu.nf.meituan.service.exception.CodeException;
import edu.nf.meituan.service.exception.LoginException;
import edu.nf.meituan.service.rider.RiderService;
import edu.nf.util.sms.SMSUtil;

/**
 * @Date 2023-06-20
 * @Author qiu
 * 骑手业务实现类
 */
public class RiderServiceImpl implements RiderService {
    /**
     * 骑手登录
     *
     * @param riderName 骑手姓名
     * @return
     */
    @Override
    public Rider loginRider(String riderName, String phone) {

        Rider rider = new RiderDAOImpl().loginRider(riderName);
        if (riderName.equals(rider.getRiderName()) && phone.equals(rider.getRiderPhone())) {
            return rider;
        }
        throw new LoginException(500, "账号或密码错误");

    }


    /**
     * 骑手注册
     *
     * @param riderName  姓名
     * @param riderPhone 电话
     * @param riderType  类型
     * @param key        验证码
     * @return
     */
    @Override
    public int registerRider(String riderName, String riderPhone, String riderType, String key) {

        int row = 0;

        Rider rider = new RiderDAOImpl().equeryRider(riderPhone);
        // 判断是手机号是否注册
        if (rider != null) {
            throw new RuntimeException("该手机号已注册");
        }

        // 判断输入的验证码和生成的验证码是否一致
        String send = new SMSUtil().send(riderPhone);
        if (key.trim().equals(send)) {
            row = new RiderDAOImpl().registerRider(riderName, riderPhone, riderType);
            return row;
        }
        try {
            throw new CodeException("验证码错误");
        } catch (CodeException e) {
            throw new RuntimeException(e);
        }

    }
}
